package base_04_annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 注解实现的原理
 <pre>
 1、注解是一个实现了Annotation的“接口”
 2、使用动态代理
 </pre>
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 * copyright (C), 2013-2022, 广州九尾信息科技有限公司
 * @date 2022/3/25 14:38
 */
@MethodAnnotation
public class MethodAnnotationDemo {
    @MethodAnnotation
    private int i;
    @MethodAnnotation(value = "方法名m1", description = "void方法")
    public void m1() {
        System.out.println(">>>方法1");
    }

    @MethodAnnotation(value = "方法名m2", description = "静态void方法")
    public static void m2() {
        System.out.println(">>>方法2");
    }

    @MethodAnnotation(value = "方法名m3", description = "int方法")
    public int m3(int i) {
        System.out.println(">>>方法名m3");
        return i;
    }

    @MethodAnnotation(value = "方法名m4", description = "静态in方法")
    @InheritedAnnotation
    public static String m4(String s) {
        System.out.println(">>>方法4");
        return s;
    }

    public static void main(String[] args) throws Exception {
        //获取类中所有方法类
        Method[] methods = MethodAnnotationDemo.class.getClassLoader().loadClass("base_04_annotation.MethodAnnotationDemo").getMethods();
        for (Method method : methods) {
            if (method.isAnnotationPresent(MethodAnnotation.class)) {
                //获取指定注解
                MethodAnnotation anno = method.getAnnotation(MethodAnnotation.class);
                System.out.println(anno.value() + ">>" + anno.description());
                Annotation[] annotations = method.getAnnotations();
                for (Annotation annotation : annotations) {
                    System.out.println(method.getName() + ":" + annotation.toString());

                }
                System.out.println(">>>>>>>");
            }
        }
    }
}
